package entity

import (
	"time"

	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

//XFZXQYDKDR 新放中小企业贷款导入
type XFZXQYDKDR struct {
	ID     int        `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	KHYF   int        `gorm:"column:KHYF"`
	ISKJWH string     `gorm:"column:ISKJWH;size:200"`
	KHMC   string     `gorm:"column:KHMC;size:200"`
	QYGM   string     `gorm:"column:QYGM;size:200"`
	DBFS   string     `gorm:"column:DBFS;size:200"`
	JJH    string     `gorm:"column:JJH;size:200"`
	QSR    *time.Time `gorm:"column:QSR;type:date;"`
	DQR    *time.Time `gorm:"column:DQR;type:date;"`
	FKJE   float64    `gorm:"column:FKJE"`
	FDLL   float64    `gorm:"column:FDLL"`
	ZXLL   float64    `gorm:"column:ZXLL"`
	ISXD   string     `gorm:"column:ISXD;size:200"`
	DRJG   string     `gorm:"column:DRJG;size:200"`
}

//XfzxqydkBeforeImportValidation 导入前执行，验证数据
func XfzxqydkBeforeImportValidation(index int, rowdatas []string) []base.ErrorData {
	sql := "SELECT ID FROM T_COMP_CLIE WHERE CUNM=?"
	rows, _ := base.DB.Raw(sql, rowdatas[1]).Rows()
	var CUID int
	if rows.Next() {
		rows.Scan(&CUID)
	}
	errDatas := make([]base.ErrorData, 1, 1)
	if CUID == 0 {
		errInfos := make([]base.ErrorInfo, 1, 1)
		errinfo := base.ErrorInfo{
			ColIndex: 1,
			Message:  "客户名称错误错误或不存在！",
		}
		errInfos[0] = errinfo
		errData := base.ErrorData{
			RowIndex:   index,
			DataValues: rowdatas,
			ErrorInfos: errInfos,
		}
		errDatas[0] = errData
		return errDatas
	}
	return nil
}

//XfzxqydkAfterImport 导入后执行
func XfzxqydkAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	// err = tx.Exec("UPDATE T_BASE_YXFY_XFZXQYDK SET CUID=B.ID "+
	// 	"FROM T_BASE_YXFY_XFZXQYDK A INNER JOIN T_COMP_CLIE B ON  RTRIM(LTRIM(A.KHMC)) = B.CUNM "+
	// 	"WHERE KHYF=?", params["KHYF"]).Error
	// if err != nil {
	// 	return err
	// }
	prams := make([]interface{}, 1)
	prams[0] = params["KHYF"]
	tx.Exec("EXEC UP_BASE_YXFY_XFZXQYDK_DTIP ?", prams...)
	return nil
}

//TableName 新放中小企业贷款导入表
func (XFZXQYDKDR) TableName() string {
	return "T_BASE_YXFY_XFZXQYDK_DTIP"
}

//XFZXQYDKDREF 返回结构体
func XFZXQYDKDREF() base.EntityFunc {
	return func() interface{} {
		entity := XFZXQYDKDR{}
		return &entity
	}
}

//XFZXQYDKDRESF 返回结构体数组
func XFZXQYDKDRESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []XFZXQYDKDR
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("XFZXQYDKDR", base.NewDefaultDao(XFZXQYDKDREF(), XFZXQYDKDRESF(), nil))
	//base.SetValidationCallBack("XFZXQYDKDR", XfzxqydkBeforeImportValidation)
	base.SetAfterCallBack("XFZXQYDKDR", XfzxqydkAfterImport)
}
